package com.travel.Controller.User;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import com.travel.Bean.User;
import com.travel.Service.Impl.UserServiceImpl;
import com.travel.util.Mail;
import com.travel.util.R;
import com.travel.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("User")
@Api("用户相关接口")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private Mail mail;

    @ApiOperation("用户注册接口")
    @PostMapping("/UserRegister")
    public R UserRegister(User user) throws Exception {

        if(userService.userRegistration(user))
            return R.ok(0,"注册成功");
        return R.no(1,"注册失败，请联系管理员");
    }


    /**
     * 邮件验证码
     *
     * @param response 响应
     * @param userMail 用户邮件
     * @return {@link R}
     * @throws IOException ioexception
     */
    @ApiOperation("验证码")
    @PostMapping("MailCaptcha")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "KeyName",value = "验证码发送的目标值（邮箱，手机号等）")
    })
    public R MailCaptcha(HttpServletResponse response,String userMail) throws IOException {
        //检查邮箱是否已被使用
        if(userService.userMailRepetition(userMail))
            return R.no(2,"邮箱已注册");

        //定义图形验证码的长、宽、验证码字符数、干扰线宽度
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(150, 40, 5, 4);
        //图形验证码写出，可以写出到文件，也可以写出到流
        captcha.write(response.getOutputStream());
        //获取验证码中的文字内容
        String verifyCode = captcha.getCode();
        RedisUtil redisUtil = new RedisUtil();
        try{
            //发送邮箱验证码
            mail.sendMail(userMail,"验证码："+verifyCode);
            //将发送的验证码存入redis
            redisUtil.set(userMail,verifyCode,10, TimeUnit.MINUTES);
        }catch (Exception e){
            return R.no(1,"验证码发送失败");
        }
        return R.ok(0,"验证码十分钟有效");
    }


    /**
     * 验证码
     *
     * @param response 响应
     * @param session  会话
     * @throws IOException ioexception
     */
    @ApiOperation("验证码")
    @RequestMapping("/Captcha")
    public void Captcha(HttpServletResponse response, HttpSession session) throws IOException {
        //定义图形验证码的长、宽、验证码字符数、干扰线宽度
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(150, 40, 5, 4);
        //图形验证码写出，可以写出到文件，也可以写出到流
        captcha.write(response.getOutputStream());
        //获取验证码中的文字内容
        String verifyCode = captcha.getCode();

        session.setAttribute("captcha",verifyCode);

    }




}
